package com.dream.keigezhushou.Activity.acty.play;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.BaseActivity;
import com.dream.keigezhushou.Activity.acty.play.fragment.HotVideoFragment;
import com.dream.keigezhushou.Activity.acty.play.fragment.NewVideoFragment;
import com.dream.keigezhushou.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HotVideoActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.iv_return)
    ImageView ivReturn;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_hot_video)
    TextView tvHotVideo;
    @BindView(R.id.tv_new_video)
    TextView tvNewVideo;
    @BindView(R.id.vp_contain)
    ViewPager vpContain;

    private ArrayList<Fragment> arrayList;
    private FragmentPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_video);
        ButterKnife.bind(this);

        initView();
        initData();
        initEvent();
    }

    //初始化界面
    protected void initView(){

        tvTitle.setText("热门视频");
        tvHotVideo.setSelected(true);
    }

    //初始化数据
    protected void initData(){

        //创建fragment的集合
        arrayList = new ArrayList<Fragment>();
        arrayList.add(new HotVideoFragment());
        arrayList.add(new NewVideoFragment());

        //创建适配器
        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return arrayList.get(position);
            }

            @Override
            public int getCount() {
                return arrayList.size();
            }
        };

        vpContain.setAdapter(adapter);

    }


    //初始化事件
    protected void initEvent(){

        //vp发生变化的监听

        vpContain.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        ivReturn.setOnClickListener(this);
        tvHotVideo.setOnClickListener(this);
        tvNewVideo.setOnClickListener(this);
    }


    //点击事件的实现

    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.iv_return:

                //返回
                finish();
                break;

            case R.id.tv_hot_video:

                tvHotVideo.setSelected(true);
                tvNewVideo.setSelected(false);
                vpContain.setCurrentItem(0);
                break;

            case R.id.tv_new_video:

                tvHotVideo.setSelected(false);
                tvNewVideo.setSelected(true);
                vpContain.setCurrentItem(1);
                break;
        }
    }
}
